package com.lagou.edu.filter;

import com.lagou.edu.service.UserServiceFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class AuthorityFilter implements GlobalFilter, Ordered {

    // 不拦截方法集合
    private static List<String> noIntercetList = new ArrayList<>();

    static {
        noIntercetList.add("user");
        noIntercetList.add("email");
    }

    @Autowired
    private UserServiceFeignClient userServiceFeignClient;


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 从上下文中取出request和response对象
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();

        MultiValueMap<String, HttpCookie> cookies= request.getCookies();
        String token="";
        if(cookies.containsKey("token")){
            token =cookies.getFirst("token").getValue();
            if (token != null && !token.isEmpty()) {
                //远程验证
                String email= userServiceFeignClient.info(token);
                if(email!=null && !email.equals("")){
                    // 合法请求，放⾏，执⾏后续的过滤器
                    return chain.filter(exchange);
                }
            }
        }

        String path = request.getPath().toString();
        if(path.startsWith("/api/user")||path.startsWith("/api/code")){
            // 合法请求，放⾏，执⾏后续的过滤器
            return chain.filter(exchange);
        }

        response.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
        response.getHeaders().add("Location", "/login.html");
        return response.setComplete();


        /*String path = request.getPath().toString();

        if(path.startsWith("/api/user")||path.startsWith("/api/code")){
            // 合法请求，放⾏，执⾏后续的过滤器
            return chain.filter(exchange);
        }else{
            MultiValueMap<String, HttpCookie> cookies= request.getCookies();
            String token="";
            if(cookies.containsKey("token")){
                token =cookies.getFirst("token").getValue();
                if (token != null && !token.isEmpty()) {
                    //远程验证
                    String email= userServiceFeignClient.info(token);
                    if(email!=null && !email.equals("")){
                        // 合法请求，放⾏，执⾏后续的过滤器
                        return chain.filter(exchange);
                    }
                }
            }
//            logger.info( "token is empty..." );
            response.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
            response.getHeaders().add("Location", "/login.html");
            return response.setComplete();
        }*/



    }

    @Override
    public int getOrder() {
        return 0;
    }
}
